{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import os\n",
    "import codecs\n",
    "from utils import nlp_utils\n",
    "from  tqdm import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "seed = 1024\n",
    "np.random.seed(seed)\n",
    "path = \"../data/\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "train = pd.read_pickle(path + \"train_clean.pkl\")\n",
    "valid = pd.read_pickle(path + \"valid_clean.pkl\")\n",
    "dev = pd.read_pickle(path+'dev_clean.pkl')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "vector_size = 100\n",
    "glove_dir =   '../data/glove.6B.{0}d.txt'.format(vector_size)\n",
    "Embedd_model = nlp_utils._get_embedd_Index(glove_dir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data_all = pd.concat([train,valid,dev])\n",
    "data_all.reset_index(inplace=1,drop=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def _wrapper_embedd(cx):\n",
    "    q = cx\n",
    "    wl = str(q).strip().lower().split()\n",
    "    centroid = np.zeros(vector_size)\n",
    "    k = 0\n",
    "    for w in wl:\n",
    "        if w in Embedd_model:\n",
    "            centroid+= Embedd_model[w]\n",
    "            k+=1\n",
    "    if k==0:\n",
    "        return np.zeros(vector_size)\n",
    "    centroid/=k\n",
    "    return centroid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 30920/30920 [00:21<00:00, 1469.68it/s]\n"
     ]
    }
   ],
   "source": [
    "embedd_fea = []\n",
    "dd = data_all['context'].values\n",
    "for it in tqdm(np.arange(data_all.shape[0])):\n",
    "    embedd_fea.append(_wrapper_embedd(dd[it]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "train_fea = embedd_fea[:train.shape[0]]\n",
    "valid_fea = embedd_fea[train.shape[0]:(train.shape[0]+valid.shape[0])]\n",
    "dev_fea = embedd_fea[(train.shape[0]+valid.shape[0]):]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "train_fea = np.array(train_fea)\n",
    "valid_fea = np.array(valid_fea)\n",
    "dev_fea = np.array(dev_fea)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.1104343 ,  0.15741599,  0.37776321, ..., -0.39904101,\n",
       "         0.32659671,  0.26112859],\n",
       "       [-0.07622395,  0.16776498,  0.4426376 , ..., -0.3269218 ,\n",
       "         0.34378412,  0.18529767],\n",
       "       [ 0.        ,  0.        ,  0.        , ...,  0.        ,\n",
       "         0.        ,  0.        ],\n",
       "       ..., \n",
       "       [-0.06625481,  0.09361166,  0.29587908, ..., -0.24043611,\n",
       "         0.3658119 ,  0.06044482],\n",
       "       [-0.16702967,  0.21480843,  0.41832681, ..., -0.4638902 ,\n",
       "         0.50210198,  0.19602787],\n",
       "       [-0.03610992,  0.19402572,  0.32895299, ..., -0.34421818,\n",
       "         0.40142664,  0.19233299]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_fea"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "pd.to_pickle(train_fea,path+'train_w2v.pkl')\n",
    "pd.to_pickle(valid_fea,path+'valid_w2v.pkl')\n",
    "pd.to_pickle(dev_fea,path+'dev_w2v.pkl')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
